package com.letoken.platform.pub.res;

import com.letoken.platform.pub.enums.ApiResponseEnum;
import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


/**
 * ApiResponse
 *
 */
@Data
@ApiModel(value = "统一接口返回对象", description = "统一接口返回对象")
public class ApiResponse<T> implements Serializable {

    @ApiModelProperty(name = "code", value = "状态码", example = "80000000")
    private int code = ApiResponseEnum.SUCCESS.getCode();

    @ApiModelProperty(name = "msg", value = "状态信息", example = "success")
    private String msg = ApiResponseEnum.SUCCESS.getMsg();

    @ApiModelProperty(name = "data", value = "业务对象")
    private T data;

    @ApiModelProperty(name = "timeStamp", value = "时间戳")
    private long timeStamp;

    public static ApiResponse create() {
        ApiResponse apiResponse = new ApiResponse();
        apiResponse.setTimeStamp(System.currentTimeMillis());
        return apiResponse;
    }

    public static ApiResponse create(Object data) {
        ApiResponse apiResponse = create();
        apiResponse.setData(data);
        return apiResponse;
    }


    public static ApiResponse error(LetokenApiResponseEnum letokenApiResponseEnum) {
        ApiResponse apiResponse = create();
        apiResponse.setCode(letokenApiResponseEnum.getCode());
        apiResponse.setMsg(letokenApiResponseEnum.getMsg());
        return apiResponse;
    }

    public static ApiResponse create(int code, String msg) {
        ApiResponse apiResponse = create();
        apiResponse.setCode(code);
        apiResponse.setMsg(msg);
        return apiResponse;
    }

    public static ApiResponse create(int code, String msg, Object data) {
        ApiResponse apiResponse = create(code, msg);
        apiResponse.setData(data);
        return apiResponse;
    }

    public static ApiResponse create(LetokenApiResponseEnum letokenApiResponseEnum) {
        return create(letokenApiResponseEnum.getCode(), letokenApiResponseEnum.getMsg());
    }
}

